// 统一管理项目用户相关的接口
import request from '@/utils/request'
// 引入ts类型
import type {
  loginFormData,
  loginResponseData,
  userInfoResponseData,
} from './type'

// 项目用户相关的请求地址
enum API {
  REGISTER_URL = '/users/register',
  LOGIN_URL = '/users/login',
  USERINFO_URL = '/users/info',
  LOGOUT_URL = '/users/logout',
  USERLIST_URL = '/users/user_list',
  SEARCHUSER_URL = '/users/get',
  DELETEUSER_URL = '/users/delete',
  GETUSER_URL = '/users/get',
  ADDUSER_URL = '/users/superuser',
  EDITNICKNAME_URL = '/users/edit_nickname',
  EDITACCOUNT_URL = '/users/edit_email',
  EDITPASSWORD_URL = '/users/user/',
  AVATAR_URL = '/users/avatars',
  UPLOADAVATAR_URL = '/users/upload_avatar',
  CAPTCHA_URL = '/users/captcha',
  FORGET_URL = '/users/forget',
  EDIPASSWORD_URL = '/users/edit_password',
  EDITINFO_URL = '/users/edit',
}

// 注册接口 泛型<请求参数类型,响应参数类型>
export const reqRegister = (data: any) =>
  request.post<any, any>(API.REGISTER_URL, data)
// 登录接口 泛型<请求参数类型,响应参数类型>
export const reqLogin = (data: any) =>
  request.post<any, any>(API.LOGIN_URL, data)
// 获取用户信息
export const reqUserInfo = () => request.get<any, any>(API.USERINFO_URL)
// 管理员获取用户列表
export const reqUserList = (page: number) =>
  request.get<any, any>(API.USERLIST_URL, { params: { page } })
// 查询某一个用户的信息
export const reqInfo = (username: string) =>
  request.get<any, any>(API.SEARCHUSER_URL, { params: { username } })
// 删除用户信息
export const reqDelete = (id: number) =>
  request.delete<any, any>(API.DELETEUSER_URL + `/${id}`)
// 根据ID获取用户
export const reqUser = (id: number) =>
  request.get<any, any>(API.GETUSER_URL + `/${id}`)
// 根据ID修改用户信息
export const reqEdit = (id: number, data: any) =>
  request.put<any, any>(API.EDITINFO_URL + `/${id}`, data)
// 当前用户修改昵称
export const reqNickname = (id: number, new_nickname: any) =>
  request.put<any, any>(`${API.EDITNICKNAME_URL}/${id}`, { new_nickname })
// 当前用户修改邮箱账号
export const reqAccount = (id: number, data: any) =>
  request.put<any, any>(`${API.EDITACCOUNT_URL}/${id}`, { data })
// 当前用户修改密码
export const reqPassword = (id: number, data: any) =>
  request.put<any, any>(`${API.EDIPASSWORD_URL}/${id}`, data)
// 当前用户上传头像
export const reqUploadAvatar = (avatar: any) =>
  request.post<any, any>(API.UPLOADAVATAR_URL, avatar)
// 获取用户头像
export const reqAvatarAPI = (name: string | null) => {
  const cleanName = name ? name.split('?')[1] : ''
  const url = cleanName
    ? `${API.AVATAR_URL}?avatar_name=${encodeURIComponent(cleanName)}`
    : API.AVATAR_URL
  return request.get<any, any>(url, { responseType: 'blob' })
}
// 退出登录
export const reqLogout = () => request.post<any, any>(API.LOGOUT_URL)
// 获取验证码
export const reqCaptchaAPI = () =>
  request.get<any, any>(API.CAPTCHA_URL, {
    responseType: 'blob', // 设置响应类型为 blob
  })
// 忘记密码
export const reqForget = (data: any) =>
  request.post<any, any>(API.FORGET_URL, data)
